<?php

declare(strict_types=1);

namespace App\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\Redis\RedisFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Psr\Container\ContainerInterface;

/**
 * @Command
 */
class ExpoRedis extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container, RedisFactory $redisFactory)
    {
        $this->container = $container;
        $this->redis = $redisFactory->get('expo');

        parent::__construct('expo-redis');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
    }

    public function handle()
    {
        $scores = $this->redis->zRevRange('praise_exhibitor', 0, -1, true);
        $exhibitos = Db::connection('expo')->table('exhibitor')->leftJoin('ex_content_2023', 'exhibitor.id', 'ex_content_2023.eid')->whereIn('eid',array_keys($scores))->get()->toArray();
        $exhibitorMap = array_column($exhibitos, null, 'eid');
        $data = [];
//        1 => ['id' => 1, 'name' => '长三角“家门口的好学校”主题展区<br>上海16区“家门口的好学校”特色展区', 'img' => '/app/edu-expo/2023/image/vlog/exhibitor/1-new1@2x.png'],
//    2 => ['id' => 2, 'name' => '高等教育展区', 'img' => '/app/edu-expo/2023/image/vlog/exhibitor/2@2x.png'],
//    3 => ['id' => 3, 'name' => '区域教育展区', 'img' => '/app/edu-expo/2023/image/vlog/exhibitor/3@2x.png'],
//    // 4 => ['id' => 4, 'name' => '高等职业教育展区', 'img' => '/app/edu-expo/2023/image/vlog/exhibitor/4@2x.png'],
//    // 5 => ['id' => 5, 'name' => '中等职业教育展区', 'img' => '/app/edu-expo/2023/image/vlog/exhibitor/5@2x.png'],
//    6 => ['id' => 6, 'name' => '企业展区', 'img' => '/app/edu-expo/2023/image/vlog/exhibitor/6@2x.png'],
//    7 => ['id' => 7, 'name' => '职业教育展区', 'img' => '/app/edu-expo/2023/image/vlog/exhibitor/7@3x.png'],
//];
        $areaMap = [
            1 => '长三角“家门口的好学校”主题展区上海16区“家门口的好学校”特色展区',
            2 => '高等教育展区',
            3 => '区域教育展区',
            6 => '企业展区',
            7 => '职业教育展区',
        ];
        foreach ($scores as $id => $score) {
            $data[] = [
                $id,
                $exhibitorMap[$id]->name,
                $areaMap[$exhibitorMap[$id]->area] ?? '',
                $exhibitorMap[$id]->desktop,
                $score,
            ];
        }
        //导出Excel文件
        $header = ['ID', '名称', '展区', '展位', '点赞数'];
        $this->exportExcel($header, $data);
    }
    public function exportExcel($header, &$data, $title = 'sheet')
    {
        $spreadsheet = new Spreadsheet();

        $spreadsheet->getProperties()
            ->setTitle($title);
        $handle = $spreadsheet->setActiveSheetIndex(0);
        foreach ($header as $index => $item) {
            $handle->setCellValue(Coordinate::stringFromColumnIndex($index + 1) . "1", $item);
        }

        $loop = 2;
        foreach ($data as $values) {
            foreach ($values as $index => $val) {
                $val = is_array($val) ? implode('，', $val) : $val;
                $handle->setCellValue(Coordinate::stringFromColumnIndex($index + 1) . $loop, $val);
            }
            $loop++;
        }

        $spreadsheet->setActiveSheetIndex(0);

        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        //注意如果当前程序是utf-8,请记加上此行，不然会出现乱码
        $writer->save('参展单位点赞排行榜.xlsx');
        exit;
    }

}
